package com.threeox.commonlibrary.entity.engine.model.base;

import android.content.Intent;

import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.entity.base.BaseObj;
import com.threeox.commonlibrary.entity.engine.event.BaseEventMessage;
import com.threeox.commonlibrary.entity.engine.event.EventMessage;
import com.threeox.commonlibrary.entity.engine.request.RequestMsg;
import com.threeox.commonlibrary.entity.engine.request.base.BaseRequestMsg;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.res.IdHelper;
import com.threeox.utillibrary.util.res.LayoutHelper;
import com.threeox.utillibrary.util.res.StringHelper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

/**
 *
 * @ClassName: BaseModelMsg
 *
 * @Description: TODO(基础的模型对象)
 *
 * @author 赵屈犇
 *
 * @date 创建时间: 2017/7/4 15:16
 *
 * @version 1.0
 *
 */
public class BaseModelMsg extends BaseObj {

    /*=============标题栏设置================*/
    private Integer alpha;// 标题栏的透明度
    private String modelTitle;// 模型标题
    private String rightText;// 右边按钮的文本
    private Integer rightIcon;// 右边的ICON
    private String rightIconName;// 右边的ICON设置
    private String leftText;// 左边按钮的文本
    private Integer leftIcon;// 左边的ICON
    private String leftIconName;// 左边的ICON设置
    private Integer titleBarColorId;// 标题栏颜色ID
    private String titleBarColorName;// 标题栏颜色名称
    private Integer titleBarTextColorId;// 标题文字颜色ID
    private String titleBarTextColorName;// 标题文字颜色名称
    private Integer leftBgDrawableId;// 左边布局资源ID
    private String leftBgDrawableName;// 左边布局资源名称
    private Integer rightBgDrawableId;// 左边布局资源ID
    private String rightBgDrawableName;// 左边布局资源名称
    private boolean isShowTitleBar = true;// 默认显示titlebar
    private boolean isShowLeftLayout = true;// 是否显示左边布局
    private boolean isShowCenterLayout = true;// 是否显示左边布局
    private boolean isShowRightLayout = true;// 是否显示右边布局
    private BaseEventMessage leftClickEvent;// 左边布局的点击事件
    private BaseEventMessage rightClickEvent;// 右边布局的点击事件
    /*=============标题栏设置================*/

    /*=============各种状态View设置================*/
    private String loadingMessage;// 加载中的提示内容
    private String notDataMessage;// 没有数据的提示内容
    private String loadFailedMessage;// 加载失败的提示内容
    private String notNetWorkMessage;// 没有网络的提示内容
    /*=============各种状态View设置================*/

    /*=============扩展类设置================*/
    private String extendName;// 扩展类全名称
    private Class extendClass;// 扩展类Class
    /*=============扩展类设置================*/

    /*=============背景设置================*/
    private Integer backgroundId;// 背景Drawable的ID
    private String backgroundName;// 背景Drawable的名称
    private Integer backgroundColorId;// 背景颜色的ID
    private String backgroundColorName;// 背景颜色的名称
    /*=============背景设置================*/

    private boolean isExecute = true;// 是否直接请求
    private boolean isFullscreen = false;// 是否全屏显示,默认不是全屏
    private boolean isShowReloadView = true;// 是否现在加载状态的View
    private boolean isShowErrorToast = true;// 是否展示请求错误时的Toast
    private boolean isFullScreenShowStatusBar = false;// 是否全屏显示并显示状态栏,默认不显示

    // 沉浸式状态栏和导航栏配置
    private boolean isNaviBarTintEnabled = true; // 是否启用系统导航栏的着色
    private boolean isStatusBarTintEnabled = true; // 是否启用系统状态栏的着色
    // 沉浸式状态栏和导航栏配置

    private RequestMsg requestMsg;// 请求对象
    private List<String> paramNames; // 所有请求参数的名称
    private List<EventMessage> eventMeessages;// 所有事件的对象

    /*=============扩展View配置=============*/
    private Integer headerLayoutId;// 头部LayoutID
    private String headerLayoutName;// 头部Layout名称
    private Integer bottomLayoutId;// 底部LayoutID
    private String bottomLayoutName;// 底部Layout名称
    /*=============扩展View配置=============*/

    private String[] broadCastActions;

    public void initData(StringHelper stringHelper, IdHelper idHelper, LayoutHelper layoutHelper) {
        this.leftText = stringHelper.getStringText(leftText);
        this.rightText = stringHelper.getStringText(rightText);
        this.modelTitle = stringHelper.getStringText(modelTitle);
        if (null != requestMsg) {
            requestMsg.initData(stringHelper);
        }
        try {
            if (EmptyUtils.isNotEmpty(extendName)) {
                this.extendName = stringHelper.getStringText(extendName);
                this.extendClass = Class.forName(this.extendName);
            }
        } catch (Exception e) {
        }
        this.headerLayoutId = layoutHelper.getLayoutId(headerLayoutName);
        this.bottomLayoutId = layoutHelper.getLayoutId(bottomLayoutName);
        if (EmptyUtils.isNotEmpty(eventMeessages)) {
            for (EventMessage eventMessage: eventMeessages) {
                eventMessage.initData(stringHelper, idHelper);
            }
        }
        /**
         * 初始化标题栏资源
         */
        if (EmptyUtils.isNotEmpty(getTitleBarColorName())) {
            this.titleBarColorId = idHelper.getIdByName(getTitleBarColorName(), IdHelper.ResType.color);
        }
        if (EmptyUtils.isNotEmpty(getTitleBarTextColorName())) {
            this.titleBarTextColorId = idHelper.getIdByName(getTitleBarTextColorName(), IdHelper.ResType.color);
        }
        if (EmptyUtils.isNotEmpty(getLeftBgDrawableName())) {
            this.leftBgDrawableId = idHelper.getIdByName(getLeftBgDrawableName(), IdHelper.ResType.drawable);
        }
        if (EmptyUtils.isNotEmpty(getRightBgDrawableName())) {
            this.rightBgDrawableId = idHelper.getIdByName(getRightBgDrawableName(), IdHelper.ResType.drawable);
        }
        /**
         * 初始化标题栏资源
         */
        if (EmptyUtils.isNotEmpty(leftClickEvent)) {
            leftClickEvent.initData(stringHelper);
        }
        if (EmptyUtils.isNotEmpty(rightClickEvent)) {
            rightClickEvent.initData(stringHelper);
        }
        if (EmptyUtils.isNotEmpty(getBackgroundName())) {
            this.backgroundId = idHelper.getIdByName(getBackgroundName(), IdHelper.ResType.drawable);
            if (EmptyUtils.isEmpty(backgroundId)) {
                this.backgroundId = idHelper.getIdByName(getBackgroundName(), IdHelper.ResType.mipmap);
            }
        }
        if (EmptyUtils.isNotEmpty(getBackgroundColorName())) {
            this.backgroundColorId = idHelper.getIdByName(getBackgroundColorName(), IdHelper.ResType.color);
        }
        if (EmptyUtils.isNotEmpty(getRightIconName())) {
            setRightIcon(idHelper.getIdByName(getRightIconName(), IdHelper.ResType.mipmap));
            if (EmptyUtils.isEmpty(getRightIcon())) {
                setRightIcon(idHelper.getIdByName(getRightIconName(), IdHelper.ResType.drawable));
            }
        }
        if (EmptyUtils.isNotEmpty(getLeftIconName())) {
            setLeftIcon(idHelper.getIdByName(getLeftIconName(), IdHelper.ResType.mipmap));
            if (EmptyUtils.isEmpty(getLeftIcon())) {
                setLeftIcon(idHelper.getIdByName(getLeftIconName(), IdHelper.ResType.drawable));
            }
        }
        if (EmptyUtils.isNotEmpty(broadCastActions)) {
            for (int i = 0; i < broadCastActions.length; i++) {
                String action = broadCastActions[i];
                action = stringHelper.getStringText(action);
                broadCastActions[i] = action;
            }
        }
        /*=============各种状态View初始化================*/
        this.loadingMessage = stringHelper.getStringText(loadingMessage);// 加载中的提示内容
        this.notDataMessage = stringHelper.getStringText(notDataMessage);// 没有数据的提示内容
        this.loadFailedMessage = stringHelper.getStringText(loadFailedMessage);// 记载失败的提示内容
        this.notNetWorkMessage = stringHelper.getStringText(notNetWorkMessage);// 没有网络的提示内容
        if (EmptyUtils.isEmpty(loadingMessage)) {
            this.loadingMessage = stringHelper.getString(R.string.loading);
        }
        if (EmptyUtils.isEmpty(notDataMessage)) {
            this.notDataMessage = stringHelper.getString(R.string.not_data_message);
        }
        if (EmptyUtils.isEmpty(notNetWorkMessage)) {
            this.notNetWorkMessage = stringHelper.getString(R.string.net_work_erro);
        }
        /*=============各种状态View初始化================*/
    }

    /*=============标题栏设置================*/

    public Integer getAlpha() {
        return alpha;
    }

    public void setAlpha(Integer alpha) {
        this.alpha = alpha;
    }

    public String getModelTitle() {
        return modelTitle;
    }

    public void setModelTitle(String modelTitle) {
        this.modelTitle = modelTitle;
    }

    public String getRightText() {
        return rightText;
    }

    public void setRightText(String rightText) {
        this.rightText = rightText;
    }

    public Integer getRightIcon() {
        return rightIcon;
    }

    public void setRightIcon(Integer rightIcon) {
        this.rightIcon = rightIcon;
    }

    public String getRightIconName() {
        return rightIconName;
    }

    public void setRightIconName(String rightIconName) {
        this.rightIconName = rightIconName;
    }

    public String getLeftText() {
        return leftText;
    }

    public void setLeftText(String leftText) {
        this.leftText = leftText;
    }

    public Integer getLeftIcon() {
        return leftIcon;
    }

    public void setLeftIcon(Integer leftIcon) {
        this.leftIcon = leftIcon;
    }

    public String getLeftIconName() {
        return leftIconName;
    }

    public void setLeftIconName(String leftIconName) {
        this.leftIconName = leftIconName;
    }

    public Integer getTitleBarColorId() {
        return titleBarColorId;
    }

    public void setTitleBarColorId(Integer titleBarColorId) {
        this.titleBarColorId = titleBarColorId;
    }

    public String getTitleBarColorName() {
        return titleBarColorName;
    }

    public void setTitleBarColorName(String titleBarColorName) {
        this.titleBarColorName = titleBarColorName;
    }

    public Integer getTitleBarTextColorId() {
        return titleBarTextColorId;
    }

    public void setTitleBarTextColorId(Integer titleBarTextColorId) {
        this.titleBarTextColorId = titleBarTextColorId;
    }

    public String getTitleBarTextColorName() {
        return titleBarTextColorName;
    }

    public void setTitleBarTextColorName(String titleBarTextColorName) {
        this.titleBarTextColorName = titleBarTextColorName;
    }

    public Integer getLeftBgDrawableId() {
        return leftBgDrawableId;
    }

    public void setLeftBgDrawableId(Integer leftBgDrawableId) {
        this.leftBgDrawableId = leftBgDrawableId;
    }

    public String getLeftBgDrawableName() {
        return leftBgDrawableName;
    }

    public void setLeftBgDrawableName(String leftBgDrawableName) {
        this.leftBgDrawableName = leftBgDrawableName;
    }

    public Integer getRightBgDrawableId() {
        return rightBgDrawableId;
    }

    public void setRightBgDrawableId(Integer rightBgDrawableId) {
        this.rightBgDrawableId = rightBgDrawableId;
    }

    public String getRightBgDrawableName() {
        return rightBgDrawableName;
    }

    public void setRightBgDrawableName(String rightBgDrawableName) {
        this.rightBgDrawableName = rightBgDrawableName;
    }

    public boolean isShowTitleBar() {
        return isShowTitleBar;
    }

    public void setIsShowTitleBar(boolean showTitleBar) {
        isShowTitleBar = showTitleBar;
    }

    public boolean isShowLeftLayout() {
        return isShowLeftLayout;
    }

    public void setIsShowLeftLayout(boolean showLeftLayout) {
        isShowLeftLayout = showLeftLayout;
    }

    public boolean isShowCenterLayout() {
        return isShowCenterLayout;
    }

    public void setIsShowCenterLayout(boolean showCenterLayout) {
        isShowCenterLayout = showCenterLayout;
    }

    public boolean isShowRightLayout() {
        return isShowRightLayout;
    }

    public void setIsShowRightLayout(boolean showRightLayout) {
        isShowRightLayout = showRightLayout;
    }

    public BaseEventMessage getLeftClickEvent() {
        return leftClickEvent;
    }

    public void setLeftClickEvent(BaseEventMessage leftClickEvent) {
        this.leftClickEvent = leftClickEvent;
    }

    public BaseEventMessage getRightClickEvent() {
        return rightClickEvent;
    }

    public void setRightClickEvent(BaseEventMessage rightClickEvent) {
        this.rightClickEvent = rightClickEvent;
    }

    /*=============标题栏设置================*/


    /*=============各种状态View设置================*/

    public String getLoadingMessage() {
        return loadingMessage;
    }

    public void setLoadingMessage(String loadingMessage) {
        this.loadingMessage = loadingMessage;
    }

    public String getNotDataMessage() {
        return notDataMessage;
    }

    public void setNotDataMessage(String notDataMessage) {
        this.notDataMessage = notDataMessage;
    }

    public String getLoadFailedMessage() {
        return loadFailedMessage;
    }

    public void setLoadFailedMessage(String loadFailedMessage) {
        this.loadFailedMessage = loadFailedMessage;
    }

    public String getNotNetWorkMessage() {
        return notNetWorkMessage;
    }

    public void setNotNetWorkMessage(String notNetWorkMessage) {
        this.notNetWorkMessage = notNetWorkMessage;
    }

    /*=============各种状态View设置================*/


    /*=============扩展类设置================*/

    public String getExtendName() {
        return extendName;
    }

    public void setExtendName(String extendName) {
        this.extendName = extendName;
    }

    public Class getExtendClass() {
        return extendClass;
    }

    public void setExtendClass(Class extendClass) {
        this.extendClass = extendClass;
    }

    /*=============扩展类设置================*/

    /*=============背景设置================*/

    public Integer getBackgroundId() {
        return backgroundId;
    }

    public void setBackgroundId(Integer backgroundId) {
        this.backgroundId = backgroundId;
    }

    public String getBackgroundName() {
        return backgroundName;
    }

    public void setBackgroundName(String backgroundName) {
        this.backgroundName = backgroundName;
    }

    public Integer getBackgroundColorId() {
        return backgroundColorId;
    }

    public void setBackgroundColorId(Integer backgroundColorId) {
        this.backgroundColorId = backgroundColorId;
    }

    public String getBackgroundColorName() {
        return backgroundColorName;
    }

    public void setBackgroundColorName(String backgroundColorName) {
        this.backgroundColorName = backgroundColorName;
    }

    /*=============背景设置================*/


    public List<EventMessage> getEventMeessages() {
        return eventMeessages;
    }

    public void setEventMeessages(List<EventMessage> eventMeessages) {
        this.eventMeessages = eventMeessages;
    }

    public boolean isExecute() {
        return isExecute;
    }

    public void setIsExecute(boolean exec) {
        isExecute = exec;
    }

    public boolean isShowReloadView() {
        return isShowReloadView;
    }

    public void setIsShowReloadView(boolean showReloadView) {
        isShowReloadView = showReloadView;
    }

    public boolean isShowErrorToast() {
        return isShowErrorToast;
    }

    public void setIsShowErrorToast(boolean showErrorToast) {
        isShowErrorToast = showErrorToast;
    }

    public boolean isFullscreen() {
        return isFullscreen;
    }

    public void setIsFullscreen(boolean fullscreen) {
        isFullscreen = fullscreen;
    }

    public boolean isNaviBarTintEnabled() {
        return isNaviBarTintEnabled;
    }

    public void setIsNaviBarTintEnabled(boolean naviBarTintEnabled) {
        isNaviBarTintEnabled = naviBarTintEnabled;
    }

    public boolean isFullScreenShowStatusBar() {
        return isFullScreenShowStatusBar;
    }

    public void setIsFullScreenShowStatusBar(boolean fullScreenShowStatusBar) {
        isFullScreenShowStatusBar = fullScreenShowStatusBar;
    }

    public boolean isStatusBarTintEnabled() {
        return isStatusBarTintEnabled;
    }

    public void setIsStatusBarTintEnabled(boolean statusBarTintEnabled) {
        isStatusBarTintEnabled = statusBarTintEnabled;
    }

    public List<String> getParamNames() {
        return paramNames;
    }

    public void setParamNames(List<String> paramNames) {
        this.paramNames = paramNames;
    }

    public RequestMsg getRequestMsg() {
        return requestMsg;
    }

    public void setRequestMsg(RequestMsg requestMsg) {
        this.requestMsg = requestMsg;
    }

    /**
     * 扩展View配置
     */
    public Integer getHeaderLayoutId() {
        return headerLayoutId;
    }

    public void setHeaderLayoutId(Integer headerLayoutId) {
        this.headerLayoutId = headerLayoutId;
    }

    public String getHeaderLayoutName() {
        return headerLayoutName;
    }

    public void setHeaderLayoutName(String headerLayoutName) {
        this.headerLayoutName = headerLayoutName;
    }

    public Integer getBottomLayoutId() {
        return bottomLayoutId;
    }

    public void setBottomLayoutId(Integer bottomLayoutId) {
        this.bottomLayoutId = bottomLayoutId;
    }

    public String getBottomLayoutName() {
        return bottomLayoutName;
    }

    public void setBottomLayoutName(String bottomLayoutName) {
        this.bottomLayoutName = bottomLayoutName;
    }

    /**
     * 扩展View配置
     */

    public String[] getBroadCastActions() {
        return broadCastActions;
    }

    public void setBroadCastActions(String[] broadCastActions) {
        this.broadCastActions = broadCastActions;
    }

    /**
     * 初始化所有的参数
     *
     * @param intent
     */
    public void initAllParams(Intent intent) {
        if (null != requestMsg) {
            requestMsg.initAllParams(intent);
        }
    }

    /**
     * 初始化代替参数
     *
     * @param intent
     */
    public void initReplaceParam(Intent intent) {
        if (null != requestMsg) {
            requestMsg.initReplaceParam(intent);
        }
    }

    /**
     * 初始化请求参数
     *
     * @param intent
     */
    public void initRequestParam(Intent intent) {
        if (null != requestMsg) {
            requestMsg.initRequestParam(intent);
        }
    }


    /**
     * 初始化请求头信息
     *
     * @param intent
     */
    public void initRequestHeaders(Intent intent) {
        if (null != requestMsg) {
            requestMsg.initRequestHeaders(intent);
        }
    }

    /**
     * 初始化Sqlite 操作参数
     *
     * @param intent
     */
    public void initOpertionParams(Intent intent) {
        if (null != requestMsg) {
            requestMsg.initOpertionParams(intent);
        }
    }

    /**
     * 得到唯一的请求对象
     *
     * @return
     */
    public BaseRequestMsg getBaseRequestMsg() {
        RequestMsg requestMsg = getRequestMsg();
        BaseRequestMsg baseRequestMsg = null;
        if (requestMsg != null) {
            if (null != requestMsg.getNetwordRequest()) {
                baseRequestMsg = requestMsg.getNetwordRequest();
            } else if (null != requestMsg.getSqliteRequest()) {
                baseRequestMsg = requestMsg.getSqliteRequest();
            } else if (null != requestMsg.getFileRequest()) {
                baseRequestMsg = requestMsg.getFileRequest();
            } else if (null != requestMsg.getSharedPreRequest()) {
                baseRequestMsg = requestMsg.getSharedPreRequest();
            } else if (null != requestMsg.getIntentReqMsg()) {
                baseRequestMsg = requestMsg.getIntentReqMsg();
            } else if (null != requestMsg.getCustomRequest()) {
                baseRequestMsg = requestMsg.getCustomRequest();
            }
        }
        return baseRequestMsg;
    }

}
